<template>
  <el-dialog
    v-loading="loading"
    title="新增员工"
    :visible="dialogVisible"
    width="50%"
    @close="cancelSubmit"
  >
    <el-form ref="form" class="form" label-width="80px" :model="formData" :rules="formRules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择聘用形式" style="width:100%">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" placeholder="请选择部门" @focus="selectDepart" />
        <el-tree v-if="isShowTree" :data="departData" :props="defaultProps" :default-expand-all="true" @node-click="clickTreeItem" />
      </el-form-item>
      <el-form-item label="转正">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button size="small" type="primary" @click="addEmployee">确定</el-button>
      <el-button size="small" @click="cancelSubmit">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employee'
import { getAllList } from '@/api/department'
import { dataToTreeData } from '@/utils/index'
import employees from '@/utils/employees'
export default {
  name: 'AddEmployee',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      employees,
      // 表单校验数据
      formData: {
        correctionTime: '',
        timeOfEntry: '',
        departmentName: '',
        workNumber: '',
        formOfEmployment: '',
        mobile: '',
        username: ''
      },
      // 表单校验规则
      formRules: {
        username: [{ required: 'true', message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名为1-4位字符', trigger: 'blur' }],
        mobile: [{ required: 'true', message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式错误', trigger: 'blur' }],
        timeOfEntry: [{ required: 'true', message: '入职时间不能为空', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: 'true', message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: 'true', message: '部门不能为空', trigger: 'change' }]
      },
      // 部门数据 树形结构
      departData: [],
      defaultProps: {
        label: 'name'
      },
      isShowTree: false,
      loading: false
    }
  },
  methods: {
    // 新增用户
    async addEmployee() {
      await this.$refs.form.validate()
      this.loading = true
      await addEmployee(this.formData)
      this.loading = false
      // this.$emit('update:dialogVisible', false)
      // console.log(this)
      // 通过$parent进行数据重新获取以及关闭会话框 this.$parent是指组件的父组件
      this.$parent.getAllDetail()
      this.$parent.dialogVisible = false
    },
    // 关闭
    cancelSubmit() {
      // 关闭会话框
      this.$emit('update:dialogVisible', false)
      // 将表单校验清空
      this.$refs.form.resetFields()
      this.formData = {
        correctionTime: '',
        timeOfEntry: '',
        departmentName: '',
        workNumber: '',
        formOfEmployment: '',
        mobile: '',
        username: ''
      }
    },
    // 点击部门，获取相关数据，显示树形结构
    async selectDepart() {
      console.log(1)
      // 获取部门数据
      const { depts } = await getAllList()
      this.departData = dataToTreeData(depts, '')
      this.isShowTree = true
    },
    // 点击树形结构的项目
    clickTreeItem(obj) {
      // console.log(obj.name)
      this.formData.departmentName = obj.name
      this.isShowTree = false
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-form-item {
  .el-input{
    width: 60%;
  }
}
</style>
